﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Serviio.API
{
    public class ServiioTranscoding
    {
        private const string endpoint = "/rest/transcoding";
        private ServiioConnection conn = null;

        public bool TranscodingEnabled { get; set; }
        public string TranscodingFolderLocation { get; set; }
        public bool AudioDownmixing { get; set; }
        public int NumberOfThreads { get; set; }
        public bool BestVideoQuality { get; set; }

        internal ServiioTranscoding(ServiioConnection connection)
        {
            this.conn = connection;
            Refresh();
        }

        public bool Refresh()
        {
            try
            {
                XmlDocument xDoc = RESTHelper.Get(conn.Host + endpoint);

                TranscodingEnabled = Boolean.Parse(xDoc.SelectSingleNode("/transcoding/transcodingEnabled").InnerText);
                TranscodingFolderLocation = xDoc.SelectSingleNode("/transcoding/transcodingFolderLocation").InnerText;
                AudioDownmixing = Boolean.Parse(xDoc.SelectSingleNode("/transcoding/audioDownmixing").InnerText);
                NumberOfThreads = Int32.Parse(xDoc.SelectSingleNode("/transcoding/threadsNumber").InnerText);
                try
                {
                    BestVideoQuality = Boolean.Parse(xDoc.SelectSingleNode("/transcoding/bestVideoQuality").InnerText);
                }
                catch { }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool Update()
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                XmlDeclaration xDec = xDoc.CreateXmlDeclaration("1.0", null, null);
                xDoc.AppendChild(xDec);
                XmlElement xRoot = xDoc.CreateElement("transcoding");
                xDoc.AppendChild(xRoot);

                XmlElement xTranscodingEnabled = xDoc.CreateElement("transcodingEnabled");
                xTranscodingEnabled.InnerText = TranscodingEnabled.ToString();
                xRoot.AppendChild(xTranscodingEnabled);

                XmlElement xTranscodingFolderLocation = xDoc.CreateElement("transcodingFolderLocation");
                xTranscodingFolderLocation.InnerText = TranscodingFolderLocation;
                xRoot.AppendChild(xTranscodingFolderLocation);

                XmlElement xAudioDownmixing = xDoc.CreateElement("audioDownmixing");
                xAudioDownmixing.InnerText = AudioDownmixing.ToString();
                xRoot.AppendChild(xAudioDownmixing);

                XmlElement xThreadsNumber = xDoc.CreateElement("threadsNumber");
                xThreadsNumber.InnerText = NumberOfThreads.ToString();
                xRoot.AppendChild(xThreadsNumber);

                if (conn.CheckMinVersion("0.6.1"))
                {
                    XmlElement xBestVideoQuality = xDoc.CreateElement("bestVideoQuality");
                    xBestVideoQuality.InnerText = BestVideoQuality.ToString();
                    xRoot.AppendChild(xBestVideoQuality);
                }

                return RESTHelper.Put(conn.Host + endpoint, xDoc);
            }
            catch
            {
                return false;
            }
        }
    }
}
